package com.platform.open.enums;

import com.platform.comm.constants.EnumOption;
import com.platform.comm.enums.IDictEntry;
import com.platform.comm.enums.IEnumItem;
import com.platform.comm.enums.IEnumOption;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liujizheng
 * @version 1.0
 * @ClassName ServiceCreateType
 * @Description 服务创建方式
 * @date 2021/5/27 11:07
 */
@EnumOption(name = "serviceType")
public enum ServiceCreateType implements IDictEntry, IEnumItem, IEnumOption<String> {
    /**
     * 通过定制的创建服务
     */
    CUSTOM("CUSTOM", "服务定制"),
    /**
     * 通过注册创建服务
     */
    REGISTER("REGISTER", "服务注册"),
    ;

    private final String key;
    private final String title;

    ServiceCreateType(String key, String title) {
        this.key = key;
        this.title = title;
    }

    @Override
    public String getKey() {
        return this.key;
    }

    @Override
    public String getValue() {
        return this.title;
    }

    @Override
    public Object getData() {
        return this;
    }

    @Override
    public boolean is(String code) {
        return this.name().equals(code);
    }

    public static ServiceCreateType getByKey(String key){
        ServiceCreateType createType = REGISTER;
        for (ServiceCreateType serviceCreateType : ServiceCreateType.values()) {
            if(serviceCreateType.getKey().equals(key)){
                createType = serviceCreateType;
                break;
            }
        }
        return createType;
    }

    /**
     * 枚举项 “键”
     *
     * @return key 键
     */
    @Override
    public String key() {
        return name();
    }

    /**
     * 枚举项 “值”
     *
     * @return value 值
     */
    @Override
    public String value() {
        return title;
    }

    /**
     * 枚举options
     *
     * @return 枚举项组成的map
     */
    @Override
    public Map<String, String> enums() {
        return Arrays.stream(values()).collect(Collectors.toMap(IEnumOption::key, IEnumOption::value));
    }
}
